#include "Camera.h"
Camera::Camera()
{
	m_zoom=200;
	m_rotatx=1;
	m_rotaty=0.5;
	m_pLookAtShape=NULL;
	m_draging=false;
}

Camera::~Camera()
{
	;
}

void Camera::init()
{
	;
}

void Camera::beginDrag(GLdouble x,GLdouble y)
{
	m_dragBeginX=x;
	m_dragBeginY=y;
	m_draging=true;
	m_tmprotatx=m_rotatx;
	m_tmprotaty=m_rotaty;
}

void Camera::endDrag()
{
	m_draging=false;
}

void Camera::drag(GLdouble x,GLdouble y)
{
	const GLdouble movex=m_dragBeginX-x,movey=m_dragBeginY-y;
	if(m_draging)
	{
		m_rotatx=m_tmprotatx-movex/1000;
		if((m_pLookAtShape?m_pLookAtShape->m_Position[2]:0)+
			m_zoom*sin(m_tmprotaty-movey/1000)>=0)
		{
			m_rotaty=m_tmprotaty-movey/1000;
		}

		if(m_rotaty>=M_PI/2)
		{
			m_rotaty=M_PI/2;
		}
		if(m_rotaty<=-M_PI/2)
		{
			m_rotaty=-M_PI/2;
		}		
	}	
}

void Camera::zoomin()
{
	m_zoom--;
}

void Camera::zoomout()
{
	m_zoom++;
}

void Camera::lookAt(const baseShape &shape)
{
	m_pLookAtShape=&shape;
}

void Camera::lookNull()
{
	m_pLookAtShape=NULL;
}

void Camera::draw()
{
	m_Position[0]=
		(m_pLookAtShape?m_pLookAtShape->m_Position[0]:0)+
		m_zoom*sin(m_rotatx)*cos(-m_rotaty);
	m_Position[1]=
		(m_pLookAtShape?m_pLookAtShape->m_Position[1]:0)+
		m_zoom*cos(m_rotatx)*cos(-m_rotaty);

	m_Position[2]=
		(m_pLookAtShape?m_pLookAtShape->m_Position[2]:0)+
		m_zoom*sin(m_rotaty);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(m_zoom/10,1,10,10000);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(m_Position[0],m_Position[1],m_Position[2],
		(m_pLookAtShape?m_pLookAtShape->m_Position[0]:0),
		(m_pLookAtShape?m_pLookAtShape->m_Position[1]:0),
		(m_pLookAtShape?m_pLookAtShape->m_Position[2]:0)
		,0,0,1);
}